#! #!/usr/bin/python
# -*- coding:utf-8 -*-
'''author:gaoxiaojum 20190726'''
import pandas as pd
import numpy as np

def bytes_to_utf8(bytes_or_str):
    if isinstance(bytes_or_str, bytes):
        value = bytes_or_str.decode('utf-8')
    else:
        value = bytes_or_str
    return value


def csvReadWrite(url,resultList,csvFile,csvResultFile):
    '''csv 文件读取数据、请求接口、并生成结果'''

    data = pd.read_csv(csvFile, encoding="gbk").sample(10, random_state=4)
    '''csvFile 文件中有请求content这一列'''
    results = []
    temp = pd.DataFrame()
    for key in resultList:
        temp[key] = None
        temp['content'] = data['content']

    for i, row in data.iterrows():
        each_data = {"content": row['content']}
        print("each_data===>", each_data)
        res = requests.post(url, data=json.dumps(each_data))
        content = res.content
        content = bytes_to_utf8(content)
        content = json.loads(content)['data']
        # content = eval(content)
        results.append(content)
        print("results===>", results)

    temp['response'] = results
    for i, row in temp.iterrows():
        result = row['response']
        for key in result.keys():
            temp[key][i] = result[key]

    temp.to_csv(csvResultFile, index=False)



def atest():
    # url = "xxx"
    url = "http://192.168.31.24:5400/api/nlp/biddingExtraction"
    data = pd.read_csv("aaa.csv", encoding="gbk").sample(10, random_state=4)
    '''aaa.csv 中每列请求参数、表头content'''
    # resultList=['response','A字段','B字段','C字段','D字段','E字段','F字段']
    resultList = ['result', 'agent', 'purchaserAddress', 'bidding', 'agentAddress', 'purchaseBudget', 'agentTel',
                  'noticeTime', 'projectName', 'purchaserTel', 'purchaser', 'publishTime', 'biddingTime',
                  'purchaseUnit',
                  'purchaseCurrency']
    results=[]
    temp=pd.DataFrame()
    for key in resultList:
        temp[key]=None
        temp['content']=data['content']

        print("temp===>",temp)
    for i ,row in data.iterrows():
        each_data ={"content":row['content']}
        print("each_data===>",each_data)
        res =requests.post(url,data=json.dumps(each_data))
        content = res.content
        content = bytes_to_utf8(content)
        content = json.loads(content)['data']
        # content = eval(content)
        results.append(content)
        print("results===>",results)

    temp['response']=results
    for i ,row in temp.iterrows():
        result = row['response']
        for key in result.keys():
            temp[key][i] = result[key]

    temp.to_csv('30.csv', index=False)


if __name__ == '__main__':
    url = "http://192.168.31.24:5400/api/nlp/biddingExtraction"
    csvFilePath='./aaa.csv'
    data = pd.read_csv("aaa.csv", encoding="gbk").sample(10, random_state=4)
    '''aaa.csv 中每列请求参数、表头content'''
    # resultList=['response','A字段','B字段','C字段','D字段','E字段','F字段']
    resultList = ['result', 'agent', 'purchaserAddress', 'bidding', 'agentAddress', 'purchaseBudget', 'agentTel',
                  'noticeTime', 'projectName', 'purchaserTel', 'purchaser', 'publishTime', 'biddingTime',
                  'purchaseUnit',
                  'purchaseCurrency']
    csvResultFile="aaaResult.csv"
    csvReadWrite(url,resultList,csvFilePath,csvResultFile)